package com.lottery.ruok.lottrey;

import com.google.gson.Gson;
import com.lottery.ruok.lottrey.activity.BaseActivity;
import com.lottery.ruok.lottrey.common.Common;
import com.lottery.ruok.lottrey.eventbusObject.GetAccountWithPush;
import com.lottery.ruok.lottrey.eventbusObject.PrizePushInfo;
import com.lottery.ruok.lottrey.retrofitModule.response.push_info.BanlancePushInfo;
import com.lottery.ruok.lottrey.userInfo.UserInfo;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import utils.AESUtils;
import utils.Utils;

/**
 * 处理推送信息管理者
 */
public class ReceiverPushInfoManager {
    public static final int TYPE_MSG_LOTTERY_RESULT = 1;
    public static final int TYPE_MSG_BANLANCE = 2;
    private static long currGap = 0;


    public static void receive(BaseActivity activity, String text) {

        if (Utils.isEmpty(text)) {
            return;
        }

        try {
            List<String> jsons = AESUtils.appAESDecode(text);
            if (jsons == null) {
                return;
            }
            for (String json : jsons) {
                if (BuildConfig.DEBUG) {
                    System.out.println("push info:" + json);
                }

                org.json.JSONObject jsonObject = new org.json.JSONObject(json);
                if (jsonObject.has("msgType")) {
                    //普通消息
                    int msgType = jsonObject.getInt("msgType");
                    String msg = jsonObject.getString("msg");
                    handlerNormalPushInfo(msgType, msg);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }



    /**
     * 处理普通推送消息
     */
    private static void handlerNormalPushInfo(int msgType, String msg) {
        if (BuildConfig.DEBUG) {
            System.out.println("push info:===" + msg);
        }
        switch (msgType) {
            case TYPE_MSG_BANLANCE://余额更新
                if (!Utils.isEmpty(UserInfo.getToken())) {
                    receiveBanlance(msg);
                }
                break;
            case TYPE_MSG_LOTTERY_RESULT://开奖
                receiveLotteryResult(msg);
                break;
        }

    }

    private static void receiveLotteryResult(String msg) {
        Gson gson = new Gson();
        PrizePushInfo prizePushInfo = gson.fromJson(msg, PrizePushInfo.class);
        EventBus.getDefault().post(prizePushInfo);
    }

    /**
     * 接受余额信息
     *
     * @param msg
     */
    private static void receiveBanlance(String msg) {
        Gson gson = new Gson();
        BanlancePushInfo banlancePushInfo = gson.fromJson(msg, BanlancePushInfo.class);
        if (banlancePushInfo != null) {
            UserInfo.setAccount(banlancePushInfo.getBalance() + "");
            Common.today_profit = banlancePushInfo.getToday_lostwin();
            EventBus.getDefault().post(new GetAccountWithPush());
            if (BuildConfig.DEBUG) {
                System.out.println("push info:" + UserInfo.getAccount());
            }
        }
    }


}
